<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Search, Location, InfoFilled, Fold, Expand, SwitchButton, Guide } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../stores/modules/user'

const router = useRouter()
const userStore = useUserStore()
const isCollapse = ref(false)

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 退出登录功能
const logout = () => {
  userStore.clearUserInfo()
  ElMessage.success('退出登录成功')
  router.push('/login')
}

// 导航到子路由
const navigateTo = (path: string) => {
  router.push(`/home/${path}`)
}
</script>

<template>
  <div class="home">
    <el-container>
      <!-- 顶部导航 -->
      <el-header>
        <div class="header-content">
          <h2>校园路径分析系统</h2>
          <el-menu mode="horizontal" :ellipsis="false">
            <el-menu-item index="1">首页</el-menu-item>

            <el-menu-item index="3">关于系统</el-menu-item>
            <el-menu-item index="4" @click="logout" style="margin-left: auto">
              <el-icon>
                <SwitchButton />
              </el-icon>
              <span>退出登录</span>
            </el-menu-item>
          </el-menu>
        </div>
      </el-header>

      <el-container>
        <!-- 左侧工具栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="collapse-btn" @click="toggleCollapse">
            <el-icon>
              <component :is="isCollapse ? Expand : Fold" />
            </el-icon>
          </div>
          <el-menu :collapse="isCollapse" class="aside-menu" router>
            <el-menu-item index="/home/search" @click="navigateTo('search')">
              <el-icon>
                <Search />
              </el-icon>
              <template #title>设施查询</template>
            </el-menu-item>
            <el-menu-item index="/home/distance" @click="navigateTo('distance')">
              <el-icon>
                <Location />
              </el-icon>
              <template #title>距离分析</template>
            </el-menu-item>
            <el-menu-item index="/home/route" @click="navigateTo('route')">
              <el-icon>
                <InfoFilled />
              </el-icon>
              <template #title>路径查询</template>
            </el-menu-item>
            <el-menu-item index="/home/statistic" @click="navigateTo('statistic')">
              <el-icon>
                <InfoFilled />
              </el-icon>
              <template #title>统计分析</template>
            </el-menu-item>
            <el-menu-item index="/home/road-map" @click="navigateTo('road-map')">
              <el-icon>
                <Guide />
              </el-icon>
              <template #title>道路地图</template>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <!-- 子路由内容区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
.home {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.el-container {
  height: 100%;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  padding: 0;
}

.el-aside {
  background-color: #fff;
  transition: width 0.3s;
  border-right: 1px solid #dcdfe6;
}

.aside-menu {
  height: 100%;
}

.map-container {
  width: 100%;
  height: 100%;
}

.el-main {
  padding: 0;
  position: relative;
}

.collapse-btn {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-bottom: 1px solid #dcdfe6;
}

.collapse-btn:hover {
  background-color: #f5f7fa;
}
</style>
